/**
 * Copyright 2011 BlueOxygen Technology
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.meruvian.demo.devcamp.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.rest.RestActionSupport;
import org.bson.types.ObjectId;
import org.meruvian.demo.devcamp.dao.PersonDAO;
import org.meruvian.demo.devcamp.entity.Person;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ModelDriven;

/**
 * @author Dian Aditya
 * 
 */
@Action(value = "person", results = {
		@Result(name = "success", type = "redirect", location = "person/new"),
		@Result(name = "input", location = "person/person-add") })
public class PersonController extends RestActionSupport implements
		ModelDriven<Object> {

	@Autowired
	private PersonDAO dao;
	private Person person = new Person();

	private Map<String, Object> model = new HashMap<String, Object>();

	private static final Log LOG = LogFactory.getLog(PersonController.class);

	public String editNew() throws Exception {
		return "person/person-add";
	}

	public String index() throws Exception {
		model.put("persons", dao.find().asList());

		return "person/person-list";
	}

	public String show() throws Exception {
		model.clear();
		person = dao.get(person.getId());

		return "person/person-detail";
	}

	public String create() throws Exception {
		dao.save(person);

		return SUCCESS;
	}
	
	public String delete() throws Exception {
		dao.delete(person);
		
		return SUCCESS;
	}

	public Object getModel() {
		return model.size() > 0 ? model : person;
	}
}
